<?php
	$callback = $_REQUEST['callback'];

	include('connect.php');

	$connect = mysql_connect($hostname, $username, $password)
	or die('Could not connect: ' . mysql_error());

	mysql_select_db($database, $connect);

	$bool = mysql_select_db($database, $connect);
	if ($bool === False){
		print "can't find $database";
	}
	$result = mysql_query("SELECT COUNT(RowID) AS count FROM Tweets")
	or die ('Could not do count on table: ' . mysql_error());
	$row = mysql_fetch_assoc($result);
	$count = $row['count'];

	$query = "SELECT * FROM Tweets ORDER BY Confidence DESC LIMIT 0,10" ;
	
	$query2 = "SELECT * FROM Tweets ORDER BY Timestamp DESC LIMIT 0,10";
	
	$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$myTweet[] = array('Row_ID' => $row['RowID'],'Confidence' => $row['Confidence'],'Username' => $row['Username'],'Text' => $row['Text'],'Date' => $row['EventDate']);
	}
	
	$result2 = mysql_query($query2) or die("SQL Error 1: " . mysql_error());
	while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
		$myTweet2[] = array('Row_ID' => $row['RowID'],'Confidence' => $row['Confidence'],'Username' => $row['Username'],'Text' => $row['Text'],'Date' => $row['EventDate']);
	}

	if ($callback) {
	    header('Content-Type: text/javascript');
    	echo $callback . '({"top10":[' . json_encode($myTweet) . '],' . '"live":[' . json_encode($myTweet2) . ']})';
	}
	mysql_free_result($result);
?>
